package com.alan.camunda.boot.util;

import groovy.lang.Binding;
import groovy.lang.Script;

import java.util.Map;

public class GroovyUtil {

    private Binding binding;

    public GroovyUtil() {
        this.binding = new Binding();
    }

    public GroovyUtil(Map<String, Object> variables) {
        this.binding = new Binding(variables);
    }

    public void setVariable(String name, Object val) {
        this.binding.setVariable(name, val);
    }

    public Object getValue(String expression) {
        Script script = GroovyScriptFactory.getScript(expression);
        script.setBinding(this.binding);
        return script.run();
    }

    public <T> T getValue(String expression, Class<T> expectedType) {
        Script script = GroovyScriptFactory.getScript(expression);
        script.setBinding(this.binding);
        return (T) script.run();
    }

}
